import router from "@/router";

export default {
  namespaced: true,
  state: {
    viewTags: [],
  },
  mutations: {
    PUSH_VIEWTAGS(state, route) {
      let backPathIndex = state.viewTags.findIndex(
        (item) => item.fullPath == router.options.history.state.back
      );
      let target = state.viewTags.find(
        (item) => item.fullPath === route.fullPath
      );
      let isName = route.name;
      if (!target && isName) {
        if (backPathIndex == -1) {
          state.viewTags.push(route);
        } else {
          state.viewTags.splice(backPathIndex + 1, 0, route);
        }
      }
    },
    REMOVE_VIEWTAGS(state, route) {
      state.viewTags.forEach((item, index) => {
        if (item.fullPath === route.fullPath) {
          state.viewTags.splice(index, 1);
        }
      });
    },
    UPDATE_VIEWTAGS(state, route) {
      state.viewTags.forEach((item) => {
        if (item.fullPath == route.fullPath) {
          item = Object.assign(item, route);
        }
      });
    },
    UPDATE_VIEWTAGS_TITLE(state, title = "") {
      const nowFullPath = location.hash.substring(1);
      state.viewTags.forEach((item) => {
        if (item.fullPath == nowFullPath) {
          item.meta.title = title;
        }
      });
    },
    CLEAR_VIEWTAGS(state) {
      state.viewTags = [];
    },
  },
};
